﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main_Contact_ContactPage : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        displayContacts();
        if (Page.IsPostBack == false)
        {
            if (Request.QueryString["action"] != null)
            {
                deleteContact();
            }
        }
    }
    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }
    protected void displayContacts()
    {
        string command = "sp_get_contact_info";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "result");
        grdContacts.DataSource = ds.Tables["result"];
        grdContacts.DataBind();
        sqlCon.Close();
        sqlCmd.Dispose();
    }
    protected void grdContacts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grdContacts.PageIndex = e.NewPageIndex;
        grdContacts.DataBind();
    }
    protected void grdContacts_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = grdContacts.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            grdContacts.DataSource = dataView;
            grdContacts.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        displaySearchContacts();
    }
    protected void displaySearchContacts()
    {
        string command = "sp_search_contact";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlCommandBuilder.DeriveParameters(sqlCmd);
        string searchVar = this.txtSearchFor.Text.ToUpper().Replace(' ', '%');
        string searchVarIn = this.ddownFields.Text;
        sqlCmd.Parameters["@search_input"].Value = searchVar;
        sqlCmd.Parameters["@search_flag"].Value = searchVarIn;
        SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "result");
        grdContacts.DataSource = ds.Tables["result"];
        grdContacts.DataBind();
        sqlCmd.Dispose();
        sqlCon.Close();
    }

    protected void grdContacts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblEdit = (Label)e.Row.FindControl("lblEdit");
            string contactId = DataBinder.Eval(e.Row.DataItem, "contact_id").ToString();
            lblEdit.Attributes.Add("onclick", "javascript:EditContact('" + contactId + "');");

            Label lblDelete = (Label)e.Row.FindControl("lblDelete");
            lblDelete.Attributes.Add("onclick", "javascript:DeleteContact('" + contactId + "');");
        }
    }
    protected void deleteContact()
    {
        string contact_id = Request.QueryString["contact_id"];
        string command = "sp_delete_contact";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
        SqlCommandBuilder.DeriveParameters(sqlCmd);
        sqlCmd.Parameters["@contact_id"].Value = new Guid(contact_id.ToString());
        int result = Convert.ToInt32(sqlCmd.ExecuteNonQuery());
        if (result != 0)
        {
            this.RegisterStartupScript("success", "<script language='javascript'>alert('succesfully deleted contact');window.location.href='ContactPage.aspx';</script>");
        }
        else
        {
            this.RegisterStartupScript("failed", "<script language='javascript'>alert('failed')</script>");
        }
        sqlCmd.Dispose();
        sqlCon.Close();
    }
}
